/********************************************************
 Name          : main.c
 Author        : Michal, Lukasz, Maciej
 Copyright     : 
 Description   :
                                 Michal Araszkiewicz (e04) - project manager
                                 * (???) - tester
                                 * (???) - *
 **********************************************************/

// Include Files 
#include "board.h"
#include "print_funcs.h"
#include "gpio.h"
#include "usart.h"
#include "intc.h"
#include <avr32/io.h>
#include "pm.h"
// nasze:
#include "IO.h"
#include "frame.h"
#include "timer.h"

#  define EXAMPLE_TARGET_PBACLK_FREQ_HZ FOSC0  // PBA clock target frequency, in Hz

#if BOARD == EVK1100
#  define EXAMPLE_USART               (&AVR32_USART1)
#  define EXAMPLE_USART_RX_PIN        AVR32_USART1_RXD_0_0_PIN
#  define EXAMPLE_USART_RX_FUNCTION   AVR32_USART1_RXD_0_0_FUNCTION
#  define EXAMPLE_USART_TX_PIN        AVR32_USART1_TXD_0_0_PIN
#  define EXAMPLE_USART_TX_FUNCTION   AVR32_USART1_TXD_0_0_FUNCTION
#  define EXAMPLE_USART_CLOCK_MASK    AVR32_USART1_CLK_PBA
#  define EXAMPLE_PDCA_CLOCK_HSB      AVR32_PDCA_CLK_HSB
#  define EXAMPLE_PDCA_CLOCK_PB       AVR32_PDCA_CLK_PBA
#endif

#if !defined(EXAMPLE_USART)             || \
    !defined(EXAMPLE_USART_RX_PIN)      || \
    !defined(EXAMPLE_USART_RX_FUNCTION) || \
    !defined(EXAMPLE_USART_TX_PIN)      || \
    !defined(EXAMPLE_USART_TX_FUNCTION)
#  error The USART configuration to use in this example is missing.
#endif

/*
void funkcja1(void){
usart_putchar(&AVR32_USART1, 'q');
}
void funkcja2(void){
usart_putchar(&AVR32_USART1, 'w');

}
void funkcja3(void){
usart_putchar(&AVR32_USART1, 'e');
}
void funkcja4(void){
usart_putchar(&AVR32_USART1, 'r');
}
void funkcja5(void){
usart_putchar(&AVR32_USART1, 't');
}
void funkcja6(void){
usart_putchar(&AVR32_USART1, 'y');
}
*/

volatile avr32_tc_t *tc = &AVR32_TC;
static const tc_waveform_opt_t WAVEFORM_OPT =
{
        .channel  = TC_CHANNEL,
        .bswtrg   = TC_EVT_EFFECT_NOOP,
        .beevt    = TC_EVT_EFFECT_NOOP,
        .bcpc     = TC_EVT_EFFECT_NOOP,
        .bcpb     = TC_EVT_EFFECT_NOOP,
        .aswtrg   = TC_EVT_EFFECT_NOOP,
        .aeevt    = TC_EVT_EFFECT_NOOP,
        .acpc     = TC_EVT_EFFECT_NOOP,
        .acpa     = TC_EVT_EFFECT_NOOP,
        .wavsel   = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,
        .enetrg   = FALSE,
        .eevt     = 0,
        .eevtedg  = TC_SEL_NO_EDGE,
        .cpcdis   = FALSE,
        .cpcstop  = FALSE,
        .burst    = FALSE,
        .clki     = FALSE,
        .tcclks   = TC_CLOCK_SOURCE_TC3 // connected to fPBA / 8.
};
static const tc_interrupt_t TC_INTERRUPT=
{
        .etrgs = 0,
        .ldrbs = 0,
        .ldras = 0,
        .cpcs  = 1,
        .cpbs  = 0,
        .cpas  = 0,
        .lovrs = 0,
        .covfs = 0
};
/*
__attribute__((__interrupt__)) void timerHandler(void){
	timerHandler1();
}
*/
__attribute__((__interrupt__)) void interruptJoystick(void){
	interruptJoystick1();
}


int main(void) {

        if (masterSlaveFlag == 1) // wlaczenie diody mastera przy starcie
                LED_On(LEDS[6]);

        pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);




        //USART options
        static const gpio_map_t USART_GPIO_MAP =
                  {
                    {5, 0},
                    {6, 0},
                    {0, 0},
                    {1, 0}
                  };

                  // USART options.
                  static const usart_options_t USART_OPTIONS =
                  {
                    .baudrate     = 9600, // 57600 dziala
                    .charlength   = 8,
                    .paritytype   = USART_NO_PARITY,
                    .stopbits     = USART_1_STOPBIT,
                    .channelmode  = USART_NORMAL_CHMODE
                  };


                  // Assign GPIO to USART.
                    gpio_enable_module(USART_GPIO_MAP, sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0]));

                    // Initialize USART in RS232 mode.
                    usart_init_rs232(&AVR32_USART1, &USART_OPTIONS, EXAMPLE_TARGET_PBACLK_FREQ_HZ);
                    usart_init_rs232(&AVR32_USART0, &USART_OPTIONS, EXAMPLE_TARGET_PBACLK_FREQ_HZ);


        initTimer();
        initBlaBlab();
        initUsartTimer();

        // joy

        gpio_enable_pin_interrupt(GPIO_JOYSTICK_PUSH, GPIO_PIN_CHANGE);
        gpio_enable_pin_interrupt(GPIO_JOYSTICK_LEFT, GPIO_RISING_EDGE);
        gpio_enable_pin_interrupt(GPIO_JOYSTICK_RIGHT, GPIO_RISING_EDGE);
        gpio_enable_pin_interrupt(GPIO_JOYSTICK_UP, GPIO_RISING_EDGE);
        gpio_enable_pin_interrupt(GPIO_JOYSTICK_DOWN, GPIO_RISING_EDGE);
        // buttons
        gpio_enable_pin_interrupt(GPIO_PUSH_BUTTON_0, GPIO_RISING_EDGE);
        gpio_enable_pin_interrupt(GPIO_PUSH_BUTTON_1, GPIO_RISING_EDGE);
        gpio_enable_pin_interrupt(GPIO_PUSH_BUTTON_2, GPIO_RISING_EDGE);

        Disable_global_interrupt();
        INTC_init_interrupts();
        // usart
        INTC_register_interrupt( (__int_handler)&uartHandler, AVR32_USART1_IRQ, AVR32_INTC_INT3);
        INTC_register_interrupt( (__int_handler)&uartHandler, AVR32_USART0_IRQ, AVR32_INTC_INT3);
        // timer
        INTC_register_interrupt( (__int_handler)&timerHandler, AVR32_TC_IRQ0, AVR32_INTC_INT0);
        // joystick
		INTC_register_interrupt( (__int_handler)&interruptJoystick, AVR32_GPIO_IRQ_0 + (GPIO_JOYSTICK_PUSH/8), AVR32_INTC_INT2);
        INTC_register_interrupt( (__int_handler)&interruptJoystick, AVR32_GPIO_IRQ_0 + (GPIO_JOYSTICK_LEFT/8), AVR32_INTC_INT2);
        INTC_register_interrupt( (__int_handler)&interruptJoystick, AVR32_GPIO_IRQ_0 + (GPIO_JOYSTICK_RIGHT/8), AVR32_INTC_INT2);
        INTC_register_interrupt( (__int_handler)&interruptJoystick, AVR32_GPIO_IRQ_0 + (GPIO_JOYSTICK_UP/8), AVR32_INTC_INT2);
        INTC_register_interrupt( (__int_handler)&interruptJoystick, AVR32_GPIO_IRQ_0 + (GPIO_JOYSTICK_DOWN/8), AVR32_INTC_INT2);
        // buttons
        INTC_register_interrupt( (__int_handler)&interruptButtons, AVR32_GPIO_IRQ_0 + (GPIO_PUSH_BUTTON_0/8), AVR32_INTC_INT1);
        INTC_register_interrupt( (__int_handler)&interruptButtons, AVR32_GPIO_IRQ_0 + (GPIO_PUSH_BUTTON_1/8), AVR32_INTC_INT1);
        INTC_register_interrupt( (__int_handler)&interruptButtons, AVR32_GPIO_IRQ_0 + (GPIO_PUSH_BUTTON_2/8), AVR32_INTC_INT1);

        Enable_global_interrupt();

        disableTransfer = 1; // nic z usarta nie wychodzi
        disableJoyInterrupts (); // zablokowanie przerwan od joya kiedy nie ma slaveow


        tc_init_waveform(tc, &WAVEFORM_OPT);
        tc_write_rc(tc, TC_CHANNEL, (FPBA / 8) / 1000); // powinno byc 1ms, POWINNO :)
        tc_configure_interrupts(tc,TC_CHANNEL,&TC_INTERRUPT);
        tc_start(tc, TC_CHANNEL);

        /*// Arash
        int i1,i2,i3,i4,i5,i6;
        i1 = setTimer(funkcja1, 1000, 1);
        i2 = setTimer(funkcja2, 1000, 0);
        i3 = setTimer(funkcja3, 5000, 0);
        i4 = setTimer(funkcja4, 1000, 0);
        i5 = setTimer(funkcja5, 1100, 1);
        i6 = setTimer(funkcja6, 3000, 0);
        startTimer( i1 );
        startTimer( i2 );
        startTimer( i3 );
        startTimer( i4 );
        startTimer( i5 );
        startTimer( i6 );
        //stopTimer(i3);
        //delTimer(i3);
        // Arash*/


        // USART
        (&AVR32_USART1)->ier = AVR32_USART_IER_RXRDY_MASK;
        (&AVR32_USART0)->ier = AVR32_USART_IER_RXRDY_MASK;
        //usart_write_line(&AVR32_USART1, "START USART 1\n");
        //usart_write_line(&AVR32_USART0, "START USART 0\n");

        while(1){}
        return 0;
}
